
package synology.ftp;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import synology.SynoBean;

public class SynoFTPClient
{
	private SynoBean bean;
	private FTPClient ftp;


	//--------------------------------------------------------------------------------------------
	public SynoFTPClient(SynoBean bean)
	{
		this.bean = bean;
		ftp = new FTPClient();
	}


	//--------------------------------------------------------------------------------------------
	public void connect()
	throws Exception
	{
		ftp.connect(bean.getUrl());
	}


	//--------------------------------------------------------------------------------------------
	public void login()
	throws Exception
	{
		boolean logged = ftp.login(bean.getLogin(), bean.getPassword());
		if (!logged)
			throw new Exception("Invalid login/password");
		
		ftp.changeWorkingDirectory("/");
	}


	//--------------------------------------------------------------------------------------------
	public void logout()
	throws Exception
	{
		boolean loggedOut = ftp.logout();
		if (!loggedOut)
			throw new Exception("Login out failed");
	}


	//--------------------------------------------------------------------------------------------
	public void disconnect()
	throws Exception
	{
		ftp.disconnect();
	}


	//--------------------------------------------------------------------------------------------
	public FTPFile[] listFiles(String pathname)
	throws IOException
	{
		return ftp.listFiles(pathname);
	}
}
